﻿using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;
using MSH.Common.Classes;
using MSH.Common.DAO.Results;
using WP7Client.Helpers;
using WP7Client.ServerManager;
using WP7Client.UserInterface.Pages;

namespace WP7Client.Controller
{
    public class LoginController : BaseController
    {
        #region Members

        private String _Username;
        private String _Password;

        private Boolean _Starting = true;

        #endregion

        #region Get&Set

        public ICommand LoginCommand 
        { 
            get; 
            internal set; 
        }

        public String Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        public String Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        public Boolean Starting
        {
            get { return _Starting; }
            set { _Starting = value; }
        }

        #endregion

        public LoginController()
        {
            LoginCommand = new RelayCommand(LoginExecute);

            Object t = UserHelper.Token;
            if (t != null)
            {
                CheckServer(); //Wanneer response, doorsturen anar OverviewPage.
            }
            else
            {
                Starting = false;
            }
        }

        /// <summary>
        /// Controlleer of de server online is
        /// </summary>
        private void CheckServer()
        {
            Loading = true;

            HttpGet checkServerHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "IsOnline"), typeof(Boolean));
            checkServerHttpGet.FinishedGet += checkServerHttpGet_FinishedGet;
        }

        void checkServerHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return;
                NavigationHelper.NavigateTo("/UserInterface/Pages/OverviewPage.xaml?");
            }
            finally
            {
                Loading = false;
            }
        }

        /// <summary>
        /// Log in met een geregistreerd account
        /// </summary>
        public void LoginExecute()
        {
            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password))
            {
                GlobalHelper.ShowMessage("Error", "Please enter your username and password.");
                return;
            }

            Loading = true;
            UserHelper.Username = Username;
            UserHelper.Password = GlobalHelper.MD5(Password);

            HttpGet loginHttpGet = new HttpGet(String.Format(GlobalHelper.Url + "Login/{0}/{1}/WindowsPhone7", UserHelper.Username, UserHelper.Password), typeof(LoginResult));
            loginHttpGet.FinishedGet += loginHttpGet_FinishedGet;
        }

        void loginHttpGet_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return;

                LoginResult result = (LoginResult)get.resultObject;

                switch (result.Result)
                {
                    case LoginResultEnum.Failure:
                        GlobalHelper.ShowMessage("Error", "Incorrect username or password.");
                        break;
                    case LoginResultEnum.NotActivated:
                        NavigationHelper.NavigateTo("/UserInterface/Pages/ActivationPage.xaml");
                        break;
                    case LoginResultEnum.Success:
                        UserHelper.Token = result.Token;
                        NavigationHelper.NavigateTo("/UserInterface/Pages/OverviewPage.xaml");
                        break;
                    case LoginResultEnum.NotActivatedAndExpired:
                        NavigationHelper.NavigateTo("/UserInterface/Pages/ResendActivationPage.xaml");
                        break;
                }
            }
            finally
            {
                Loading = false;
            }
        }         
    }
}
